
package com.ryan.notify.business;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.ryan.notify.model.NotificationRecord;
import com.ryan.notify.model.NotifyModel;
import com.ryan.notify.su.SuCmd;
import com.ryan.notify.su.SuFailException;
import com.ryan.util.LogUtil;

public class DumpNotify {

    public static NotifyModel dump() throws SuFailException {
        boolean dump = SuCmd.getInstance().execSuCmd(new String[] {
            "dumpsys notification > /sdcard/notifytmp"
        });
        
        if(!dump){
            throw new SuFailException();
        }
        
        NotifyModel model = new NotifyModel(null, null);
        
        try {
            File f = new File("/sdcard/notifytmp");
            BufferedReader br = new BufferedReader(new FileReader(f));
            String line;
            
            ArrayList<NotificationRecord> notifyList = new ArrayList<NotificationRecord>();
            while((line = br.readLine())!=null){
                LogUtil.v("E", line);
                if(line.indexOf("Notification List:")>=0){
                    boolean aNotify = false;
                    NotificationRecord record = null;
                    while((line = br.readLine())!=null){
                        if(line.indexOf("    NotificationRecord")>=0){
                            if(record!=null && record.getIcon()>0){
                                add(record, model);
                                notifyList.add(record);
                            }
                            record = new NotificationRecord();
                            aNotify = true;
                            String pkg = line.substring(line.indexOf("pkg=")+4,line.indexOf("id=")-1);
                            record.setPkg(pkg);
//                            System.out.println("-------------"+pkg);
                        }
                        if(aNotify && line.indexOf("      icon=")>=0){
                            String icon = line.substring(line.indexOf("      icon=")+11,line.indexOf(" / "));
//                            System.out.println("-----------"+icon);
                            record.setIcon(Integer.valueOf(icon.substring(2),16));
                        }
                        if(aNotify && line.indexOf("      flags=")>=0){
                            String flags = line.substring(line.indexOf("      flags=")+12);
//                            System.out.println("-----------"+flags);
                            record.setFlag(Integer.valueOf(flags.substring(2),16));
                        }
                        if(aNotify && line.indexOf("      tickerText=")>=0){
                            String tickerText = line.substring(line.indexOf("      tickerText=")+17);
//                            System.out.println("-----------"+tickerText);
                            record.setTickerText(tickerText);
                        }
                        if("".equals(line)){
                            break;
                        }
                    }
                    if(record!=null && record.getIcon()>0){
                        add(record, model);
                    }
                    break;
                }
            }
//            System.out.println("-------------"+notifyList);
            
            f.delete();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return model;   
    }
    
    private static void add(NotificationRecord data,NotifyModel model){
        if(data==null || data.getIcon()==0){
            return ;
        }
        
        for (NotificationRecord nr : model.getOngoing()) {
            if(nr.getPkg().equals(data.getPkg())){
                return ;
            }
        }
        
        for (NotificationRecord nr : model.getNormal()) {
            if(nr.getPkg().equals(data.getPkg())){
                return ;
            }
        }

        if( (data.getFlag() &0xF) == 2){
            model.getOngoing().add(data);
        }else{
            model.getNormal().add(data);
        }
        
    }
}
